#include "../import/head.h"
#include "config.h"
#include "../import/import.h"
#include "../data_struct/head.h"
#include "../global/head.h"
#include "../macro/head.h"
#include "../msg/head.h"

////////////////////////////////////////////////////////////////////////////////////////////
// 

namespace xos_sgsj
{

    //////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    config::config()
    {
        // server addr
        memset( tcp_server_ip, 0, sizeof( tcp_server_ip ) );
        tcp_server_port = 5000;
        udp_server_port = 5000;

        // net param
        tcp_max_post_accept = 10;
        tcp_max_post_recv = 5;
        udp_max_post_recv = 5;
        net_timeout_s = 60;

        // heart
        heart_check_interval_ms = 1000;
        sys_heart_interval_ms = 100;
        show_info_interval_s = 10;
    }

    config::~config()
    {
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // proc

    int config::load()
    {
        int ret = 0;

        xos_xml::i_xml_node * pRoot = 0;
        xos_xml::i_xml_doc * pDoc = 0;
        char szFile[4096] = { 0 };

        if( 0 == ret )
        {
            g_pSysData->m_os.m_pCrt->strcpy( szFile, g_pSysData->m_os.m_szExePath );
            g_pSysData->m_os.m_pMisc->path_append( szFile, "sgsj_client.xml" );
        }

        if( 0 == ret )
        {
            ret = g_pSysData->m_os.m_pXmlMgr->create_doc( &pDoc );
        }

        if( 0 == ret )
        {
            ret = pDoc->load_xml_file( szFile );
        }

        if( 0 == ret )
        {
            ret = pDoc->get_root_node( &pRoot );
        }

        if( 0 == ret )
        {
            ret = route_path( pRoot );
        }

        if( pRoot )
        {
            pRoot->release();
            pRoot = 0;
        }

        if( pDoc )
        {
            pDoc->release();
            pDoc = 0;
        }

        return ret;
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    int config::route_path( xos_xml::i_xml_node * pNode )
    {
        int ret = 0;

        xos_xml::i_xml_node * pChild = 0;
        bool bHasChild = false;

        if( 0 == ret )
        {
            pNode->get_child_node( &pChild );
        }

        while( ( 0 == ret ) && pChild )
        {
            bHasChild = true;
            route_path( pChild );
            xos_xml::i_xml_node * pT = pChild;
            pChild = 0;
            pT->get_next_sibling_node( &pChild );
            pT->release();
            pT = 0;
        }

        if( ( 0 == ret ) && !bHasChild )
        {
            get_value( pNode );
        }

        if( pChild )
        {
            pChild->release();
            pChild = 0;
        }

        return ret;
    }

    int config::get_value( xos_xml::i_xml_node * pNode )
    {
        int ret = 0;

        char name[1024] = { 0 }, value[1024] = { 0 };

        if( 0 == ret )
        {
            pNode->get_node_value( value, sizeof( value ), 0 );
            pNode->get_node_name( name, sizeof( name ), 0 );
        }

        if( 0 == g_pSysData->m_os.m_pCrt->stricmp( name, "tcp_server_ip" ) )
        {
            g_pSysData->m_os.m_pCrt->strcpy( tcp_server_ip, value );
        }
        else if( 0 == g_pSysData->m_os.m_pCrt->stricmp( name, "tcp_server_port" ) )
        {
            tcp_server_port = g_pSysData->m_os.m_pCrt->atoi( value );
        }
        else if( 0 == g_pSysData->m_os.m_pCrt->stricmp( name, "udp_server_port" ) )
        {
            udp_server_port = g_pSysData->m_os.m_pCrt->atoi( value );
        }
        else if( 0 == g_pSysData->m_os.m_pCrt->stricmp( name, "tcp_max_post_accept" ) )
        {
            tcp_max_post_accept = g_pSysData->m_os.m_pCrt->atoi( value );
        }
        else if( 0 == g_pSysData->m_os.m_pCrt->stricmp( name, "tcp_max_post_recv" ) )
        {
            tcp_max_post_recv = g_pSysData->m_os.m_pCrt->atoi( value );
        }
        else if( 0 == g_pSysData->m_os.m_pCrt->stricmp( name, "net_timeout_s" ) )
        {
            net_timeout_s = g_pSysData->m_os.m_pCrt->atoi( value );
        }
        else if( 0 == g_pSysData->m_os.m_pCrt->stricmp( name, "udp_max_post_recv" ) )
        {
            udp_max_post_recv = g_pSysData->m_os.m_pCrt->atoi( value );
        }
        else if( 0 == g_pSysData->m_os.m_pCrt->stricmp( name, "heart_check_interval_ms" ) )
        {
            heart_check_interval_ms = g_pSysData->m_os.m_pCrt->atoi( value );
        }
        else if( 0 == g_pSysData->m_os.m_pCrt->stricmp( name, "sys_heart_interval_ms" ) )
        {
            sys_heart_interval_ms = g_pSysData->m_os.m_pCrt->atoi( value );
        }
        else if( 0 == g_pSysData->m_os.m_pCrt->stricmp( name, "show_info_interval_s" ) )
        {
            show_info_interval_s = g_pSysData->m_os.m_pCrt->atoi( value );
        }

        return ret;
    }

} // xos_sgsj
